package com.mbc.service.impl;


import com.mbc.mapper.SignInMapper;
import com.mbc.service.SignInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户签到表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-09
 */
@Service
public class SignInServiceImpl implements SignInService {

    @Autowired
    private SignInMapper signInMapper;

    /**
     * 每天用户签到
     *
     * @param userId 用户 ID
     * @return boolean
     */
/*    @Override
    public boolean getSelectUserSignIn(String userId) {

        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        if (signInMapper.getUserId(userId) == 0) {
            return signInMapper.insertUserSignIn(userId, year, month, now) > 0;
        }
        //找到传入用户的最新时间
        LocalDateTime time = signInMapper.getUserLatestTime(userId);
        int year1 = time.getYear();
        int month1 = time.getMonthValue();
        int day1 = time.getDayOfMonth();
        if (year == year1 && month == month1) {
            if (day > day1) {
                //用户签到更新
                return signInMapper.updateUserSignIn(userId, now) > 0;
            } else {
                return false;
            }
        }
        //插入不同年份月份用户签到记录
        return signInMapper.insertUserSignIn(userId, year, month, now) > 0;
    }*/
    @Override
    public boolean getSelectUserSignIn(String userId) {

        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        if (signInMapper.getUserId(userId) == null) {
            return signInMapper.insertUserSignIn(userId, year, month, now) > 0;
        }
        //找到传入用户的最新时间
        LocalDateTime time = signInMapper.getUserLatestTime(userId);

        if (now.compareTo(time) > 0) {
            if (day > time.getDayOfMonth()) {
                //用户签到更新
                return signInMapper.updateUserSignIn(userId, now) > 0;
            } else {
                return false;
            }
        }
        //插入不同年份月份用户签到记录
        return signInMapper.insertUserSignIn(userId, year, month, now) > 0;
    }
}
